// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.warehouse;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.dengues.commons.notifications.AbstractNotification;
import org.dengues.commons.notifications.AbstractNotificationUIJob;
import org.dengues.commons.notifications.AbstractNotificationsManager;
import org.dengues.commons.notifications.INotificationProvider;
import org.dengues.core.DenguesCorePlugin;
import org.dengues.core.connectivity.IConnectivityManager;
import org.dengues.core.connectivity.IConnectivityProperty;
import org.dengues.core.prefs.IDenguesPrefsConstant;
import org.dengues.ui.DenguesUiPlugin;
import org.dengues.warehouse.i18n.Messages;
import org.dengues.warehouse.notifications.ConnPropertyNotification;
import org.dengues.warehouse.notifications.DenguesNotificationsManager;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

/**
 * The activator class controls the plug-in life cycle
 */
/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 2008-1-6 qiang.zhang $
 * 
 */
public class WarehousePlugin extends AbstractUIPlugin {

    /**
     * Qiang.Zhang.Adolf@gmail.com WarehousePlugin class global comment. Detailled comment <br/>
     * 
     * $Id: Dengues.epf 2008-1-8 qiang.zhang $
     * 
     */
    private class ConnPropertyUiInitializationJob extends AbstractNotificationUIJob {

        public ConnPropertyUiInitializationJob() {
            super(Messages.getString("WarehousePlugin.tasktitle")); //$NON-NLS-1$
        }

        /*
         * (non-Javadoc)
         * 
         * @see org.dengues.commons.notifications.AbstractNotificationUIJob#getNotificationDelay()
         */
        @Override
        protected long getNotificationDelay() {
            return NOTIFICATION_DELAY;
        }

        /*
         * (non-Javadoc)
         * 
         * @see org.dengues.commons.notifications.AbstractNotificationUIJob#getNotificationProvider()
         */
        @Override
        protected INotificationProvider getNotificationProvider() {
            return INCOMING_NOTIFICATION_PROVIDER;
        }

        /*
         * (non-Javadoc)
         * 
         * @see org.dengues.commons.notifications.AbstractNotificationUIJob#getNotificationsManager()
         */
        @Override
        protected AbstractNotificationsManager getNotificationsManager() {
            notificationsManager = new DenguesNotificationsManager();
            return notificationsManager;
        }

        /*
         * (non-Javadoc)
         * 
         * @see org.dengues.commons.notifications.AbstractNotificationUIJob#getPreferenceStore()
         */
        @Override
        protected IPreferenceStore getPreferenceStore() {
            return DenguesUiPlugin.getDefault().getPreferenceStore();
        }
    }

    private static INotificationProvider INCOMING_NOTIFICATION_PROVIDER = new INotificationProvider() {

        public Set<AbstractNotification> getNotifications() {
            Set<AbstractNotification> notifications = new HashSet<AbstractNotification>();
            List<IConnectivityProperty> allConnProperties = DenguesCorePlugin.getDefault().getDenguesUiService()
                    .getConnectivityManager().getAllConnProperties();
            for (IConnectivityProperty repository : allConnProperties) {
                ConnPropertyNotification notification = getCheckConnNotification(repository);
                notifications.add(notification);
            }
            return notifications;
        }
    };

    private static int NOTIFICATION_DELAY = 10000;

    private DenguesNotificationsManager notificationsManager;

    // The plug-in ID
    public static final String PLUGIN_ID = "org.dengues.warehouse"; //$NON-NLS-1$

    // The shared instance
    private static WarehousePlugin plugin;

    /**
     * The constructor
     */
    public WarehousePlugin() {
        plugin = this;
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getCheckConnNotification".
     * 
     * @param repository
     * @return
     */
    protected static ConnPropertyNotification getCheckConnNotification(IConnectivityProperty repository) {
        repository.validate();
        ConnPropertyNotification notification = new ConnPropertyNotification(repository);
        if (repository.isValid()) {
            IConnectivityManager manager = DenguesCorePlugin.getDefault().getDenguesUiService().getConnectivityManager();
            String message = manager.getConnection(repository.getBaseProperties(), false,
                    repository.getType().getNestedDriver() != null);
            notification.setDescription(message);
            notification.setSuccessful(manager.getConnection() != null);
        }
        notification.setDate(new Date());
        return notification;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
     */
    @Override
    public void start(BundleContext context) throws Exception {
        super.start(context);
        if (DenguesUiPlugin.getDefault().getPreferenceStore().getBoolean(IDenguesPrefsConstant.CONNECTION_AUTO_TEST)) {
            new ConnPropertyUiInitializationJob().schedule();
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
     */
    @Override
    public void stop(BundleContext context) throws Exception {
        plugin = null;
        if (PlatformUI.isWorkbenchRunning()) {
            DenguesUiPlugin.getDefault().getPreferenceStore().removePropertyChangeListener(notificationsManager);
        }
        super.stop(context);
    }

    /**
     * Returns the shared instance
     * 
     * @return the shared instance
     */
    public static WarehousePlugin getDefault() {
        return plugin;
    }

    public static ImageDescriptor getImageDescriptor(String path) {
        return imageDescriptorFromPlugin(PLUGIN_ID, path);
    }
}
